home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / trace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  4.7 KB  |  233 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "iface.h"
  7. #include "trace.h"
  8. #include "session.h"
  9.  
  10. /* Redefined here so that programs calling dump in the library won't pull
  11.  * in the rest of the package
  12.  */
  13. static char nospace[] = "No space!!\n";
  14. static char nullpak[] = "empty packet!!\n";
  15. static char if_tr_i[] = "%s recv: [%s]\n";
  16. static char if_tr_o[] = "%s sent: [%s]\n";
  17.  
  18. FILE *trfp = stdout;            /* file pointer used for tracing */
  19. int trcount = 0;                /* used to close file for flushing */
  20. char trname[40];                /* name if not stdout */
  21. int notraceall;            /* 0 = trace all, 1 = only in cmd mode */
  22. extern int mode;        /* command mode or not */
  23.  
  24. dump(interface,direction,type,bp)
  25. register struct interface *interface;
  26. int direction;
  27. unsigned type;
  28. struct mbuf *bp;
  29. {
  30.     struct mbuf *tbp;
  31.     void ascii_dump(),hex_dump();
  32.     int ax25_dump(),ether_dump(),ip_dump(),at_dump(),slfp_dump();
  33.     int (*func)();
  34.     char *cp;
  35.     long t;
  36.     int16 size;
  37.  
  38.     if((interface->trace & direction) == 0)
  39.         return;    /* Nothing to trace */
  40.  
  41.     if (notraceall && mode != CMD_MODE)
  42.         return; /* No trace while in session, if mode */
  43.  
  44.     if(bp == NULLBUF || (size = len_mbuf(bp)) == 0)
  45.         return;
  46.  
  47. #ifdef SCREEN
  48.     (void)outscreen(1);        /* DG2KK: switch to trace screen */
  49. #endif
  50.     time(&t);            /* DG2KK: get time */
  51.     cp = ctime(&t);
  52.     rip(cp);
  53.  
  54.     switch(direction){
  55.     case IF_TRACE_IN:
  56.         fprintf(trfp,if_tr_i,interface->name,cp);
  57.         break;
  58.     case IF_TRACE_OUT:
  59.         fprintf(trfp,if_tr_o,interface->name,cp);
  60.         break;
  61.     }
  62.     if(bp == NULLBUF || (size = len_mbuf(bp)) == 0){
  63.         fprintf(trfp,nullpak);
  64.         goto trdone;
  65.     }
  66.     if(type < NTRACE)
  67.         func = tracef[type];
  68.     else
  69.         func = NULLFP;
  70.  
  71.     dup_p(&tbp,bp,0,size);
  72.     if(tbp == NULLBUF){
  73.         fprintf(trfp,nospace);
  74.         if (trfp != stdout)
  75.           printf(nospace);
  76.         goto trdone;
  77.     }
  78.     if(func != NULLFP)
  79.         (*func)(&tbp,1);
  80.     if(interface->trace & IF_TRACE_ASCII){
  81.         /* Dump only data portion of packet in ascii */
  82.         ascii_dump(&tbp);
  83.     } else if(interface->trace & IF_TRACE_HEX){
  84.         /* Dump entire packet in hex/ascii */
  85.         free_p(tbp);
  86.         dup_p(&tbp,bp,0,len_mbuf(bp));
  87.         if(tbp != NULLBUF)
  88.             hex_dump(&tbp);
  89.         else {
  90.             fprintf(trfp,nospace);
  91.             if (trfp != stdout)
  92.               printf(nospace);
  93.             }
  94.     }
  95.     free_p(tbp);
  96.  
  97. #ifdef SCREEN
  98.     (void)outscreen(0);        /* switch back to main screen */
  99. #endif
  100.  
  101.       trdone:
  102.     if(trfp != stdout) {
  103. #ifndef ATARI_ST
  104.       fflush(trfp);
  105. #endif
  106. #if (defined(MSDOS) || defined(ATARI_ST))
  107.       if (++trcount > 25) {
  108.         fclose(trfp);
  109.         trfp = fopen(trname,"a");
  110.         trcount = 0;
  111.       }
  112. #endif
  113.       if (trfp == NULLFILE || ferror(trfp)) {
  114.         printf("Error on %s - trace to console\n",trname);
  115.         if (trfp != NULLFILE && trfp != stdout)
  116.           fclose(trfp);
  117.         trfp = stdout;
  118.       }
  119.     }
  120.     fflush(stdout);
  121. }
  122.  
  123. /* Dump an mbuf in hex */
  124. void
  125. hex_dump(bpp)
  126. register struct mbuf **bpp;
  127. {
  128.     int16 n;
  129.     int16 address;
  130.     void fmtline();
  131.     char buf[16];
  132.  
  133.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  134.         return;
  135.  
  136.     address = 0;
  137.     while((n = pullup(bpp,buf,sizeof(buf))) != 0){
  138.         fmtline(address,buf,n);
  139.         address += n;
  140.     }
  141. }
  142. /* Dump an mbuf in ascii */
  143. void
  144. ascii_dump(bpp)
  145. register struct mbuf **bpp;
  146. {
  147.     char c;
  148.     register int16 tot;
  149.  
  150.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  151.         return;
  152.  
  153.     tot = 0;
  154.     while(pullup(bpp,&c,1) == 1){
  155.         if((tot % 64) == 0) {
  156.             fprintf(trfp,"%04x  ",tot);
  157.         }
  158. #ifdef PC9801
  159.         if ((c >= 0x20) || (c < 0)) {
  160. #else
  161.         if(isprint(c)) {
  162. #endif
  163.             putchar(c);
  164.         } else {
  165.             putchar('.');
  166.         }
  167.         tot++;
  168.         if((tot % 64) == 0) {
  169. #ifdef PC9801
  170.             fprintf(trfp," \n");
  171. #else
  172.             fprintf(trfp,"\n");
  173. #endif
  174.         }
  175.     }
  176.     if((tot % 64) != 0) {
  177. #ifdef PC9801
  178.         fprintf(trfp," \n");
  179. #else
  180.         fprintf(trfp,"\n");
  181. #endif
  182.     }
  183. }
  184.  
  185. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  186.  * translation, e.g.,
  187.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  188.  */
  189. void
  190. fmtline(addr,buf,len)
  191. int16 addr;
  192. char *buf;
  193. int16 len;
  194. {
  195.     char line[80];
  196.     register char *aptr,*cptr;
  197.     register int16 c;
  198.     void ctohex();
  199.  
  200.     memset(line,' ',sizeof(line));
  201.     ctohex(line,(int16)hibyte(addr));
  202.     ctohex(line+2,(int16)lobyte(addr));
  203.     aptr = &line[6];
  204.     cptr = &line[55];
  205.     while(len-- != 0){
  206.         c = *buf++ & 0xff;
  207.         ctohex(aptr,c);
  208.         aptr += 3;
  209.         c &= 0x7f;
  210.         *cptr++ = isprint(c) ? c : '.';
  211.     }
  212. #ifdef PC9801
  213.     *cptr++ = ' ';
  214. #endif
  215.     *cptr++ = '\r';
  216.     *cptr++ = '\n';
  217.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  218.     /* added */
  219. }
  220. /* Convert byte to two ascii-hex characters */
  221. static
  222. void
  223. ctohex(buf,c)
  224. register char *buf;
  225. register int16 c;
  226. {
  227.     static char hex[] = "0123456789abcdef";
  228.  
  229.     buf[0] = hex[hinibble(c)];
  230.     buf[1] = hex[lonibble(c)];
  231. }
  232.  
  233.